<?php

namespace app\admin\controller;

use EasyWeChat\Factory;
use think\Controller;

class Index extends Controller
{

    public function index()
    {
        //测试号
        $config = [
            'app_id' => 'wx8c2da4cdcbc713eb',
            'secret' => 'a0f1082da34072f113c73f30024e30ee',
            'token' => 'sdfs',
            'response_type' => 'array',
        ];


        $app = Factory::officialAccount($config);

        //获取access_token
//        $accessToken = $app->access_token;
//        $token = $accessToken->getToken();
//        $token = $accessToken->getToken(true);
//        return $token['access_token'];

        $a = file_put_contents('json.php', file_get_contents("php://input"),FILE_APPEND );

//        return $a;

//        return 'success';



        $app->server->push(function ($message) {
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
//            获取发送方
//            return $message['FromUserName'];

//            file_put_contents('json.php', $message);

            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息';
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });


//        $app->server->push(function ($message) {
//            return "您好！欢迎使用 EasyWeChat!";
//        });


        $response = $app->server->serve();

        $response->send();
        exit;


    }

    public function checkSignature()
    {
        //正式号
//        $config = [
//            'app_id' => 'wxf7e4eaa2c6f1f4b9',
//            'appsecret' => 'e29dab15c74f818b5958aac2c12e33a9',
//////            'response_type' => 'array',
//        ];
////        $app = Factory::officialAccount($config);

        $postxml = file_get_contents($this->request);//读取 POST 的原始数据
        $data = simplexml_load_string($postxml, 'SimpleXMLElement', LIBXML_NOCDATA);

        file_put_contents('./a.php', $data, FILE_APPEND);

        $token = 'sdfs';

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr = $_GET['echostr'];


        $tmpArr = [$timestamp, $nonce, $token];
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);


        if ($signature == $tmpStr) {
            return $echostr;
        } else {
            return false;
        }

    }

}
